; (function (root, factory) {
    if (typeof exports === "object") {
        // CommonJS
        module.exports = exports = factory();
    } else if (typeof define === "function" && define.amd) {
        // AMD
        define("IatFileSdk", [], factory);
    } else {
        // Global (browser)
        root.IatFileSdk = factory();
    }
}(this, function () {
    var IatFileSdk = IatFileSdk || function (cfg) {
        var config = cfg || {
            "scUrl": "ws://172.31.187.27:12321/aimed/v1/saber/iat/ws",
            "frameSize": 1280,
            "common": {
                "app_id": "MIME1000"
            },
            "business": {
                "domain": "iat",
                "language": "zh_cn"
            },
            "data": {
                "format": "audio/L16;rate=16000",
                "encoding": "raw"
            }
        };

        //上传录音每帧大小
        var frameSize = config.frameSize || 1280;

        //定时发送间隔时间，单位毫秒
        var intervalTime = config.intervalTime || 5;

        //音频上传状态
        var status = 0;

        //webSocket
        var ws;

        var onMessageFun;

        var fileObj;

        //定时任务
        var interval;

        //webSocket接收消息事件
        function ws_onMessage(e) {
            if (e.data) {
                var json = JSON.parse(e.data);
                if (json && json.code == 0) {
                    if (json.data && null != json.data.status && json.data.status == 2) {
                        //当返回最后一帧时，关闭webSocket连接
                        end();
                    }
                } else {
                    end();
                }
            }
            ws_onMessageCB(e);
        }

        function ws_onMessageCB(e) {
            if (typeof onMessageFun === "function") {
                onMessageFun(e);
            }
        }

        function end() {
            clearInterval(interval);
            if (ws) {
                ws.close();
                ws = null;
            }
        }

        this.start = function (file, messageFun) {
            if (null == file) {
                return "文件为空";
            }
            onMessageFun = messageFun;
            fileObj = file;
            //重置状态
            status = 0;
            //初始化webSocket
            ws = new WebSocket(config.scUrl);
            ws.onopen = ws_onFileOpen;
            ws.onmessage = ws_onMessage;
            ws.onclose = function (e) { }
            ws.onerror = function (e) {
                end();
                var data = {
                    "code": -1,
                    "message": "WebSocket fall"
                };
                ws_onMessageCB({
                    "data": JSON.stringify(data)
                });
            }
        }

        this.stop = function (forceStop) {
            status = 2;
            if (forceStop) {
                onMessageFun = null;
                end();
            }
        }

        //文件webSocket连接成功事件
        function ws_onFileOpen() {
            fr = new FileReader();
            fr.readAsArrayBuffer(fileObj);
            fr.onload = fr_onLoad;
        }

        //FileReader加载完成事件
        function fr_onLoad() {
            var fileData = fr.result;
            var i = 0;
            interval = setInterval(function () {
                var datas = null;
                if (i >= fileData.byteLength / frameSize) {
                    status = 2;
                } else {
                    datas = new Uint8Array(fileData.slice(frameSize * i, frameSize * (i + 1)));
                }

                //首帧
                if (status == 0) {
                    var data = {};
                    data.status = 0;
                    data.format = config.data.format;
                    data.encoding = config.data.encoding;
                    data.audio = window.btoa(String.fromCharCode(...datas));
                    var frame = {};
                    frame.common = config.common;
                    frame.business = config.business;
                    frame.data = data;
                    if (ws && ws.readyState == WebSocket.OPEN) {
                        ws.send(JSON.stringify(frame));
                    }
                    status = 1;
                }//中间帧
                else if (status == 1) {
                    var data = {};
                    data.status = 1;
                    data.format = config.data.format;
                    data.encoding = config.data.encoding;
                    data.audio = window.btoa(String.fromCharCode(...datas));
                    var frame = {};
                    frame.data = data;
                    if (ws && ws.readyState == WebSocket.OPEN) {
                        ws.send(JSON.stringify(frame));
                    }
                }//最后一帧
                else {
                    clearInterval(interval);
                    var data = {};
                    data.status = 2;
                    data.format = config.data.format;
                    data.encoding = config.data.encoding;
                    data.audio = "";
                    var frame = {};
                    frame.data = data;
                    if (ws && ws.readyState == WebSocket.OPEN) {
                        ws.send(JSON.stringify(frame));
                    }
                }
                i++;
            }, intervalTime);
        }
    };
    return IatFileSdk;
}));
